package com.yesido.javabase.concurrent.jdk;

import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.*;

/**
 * @author yesido
 * @description
 * @date 2023-07-11 15:12
 * @Version 8.3
 */
public class Test {
    public static void main(String[] args) {
        // 队列长度无限制 有可能发生oom
        ExecutorService executor = Executors.newFixedThreadPool(10);
        // 线程数量无限制 有可能发生oom 堆内存溢出
        ExecutorService executor1 = Executors.newCachedThreadPool();
        // 队列长度无限
        ExecutorService executor2 = Executors.newSingleThreadExecutor();
        //
        ScheduledExecutorService executor3 = Executors.newScheduledThreadPool(5);

       /* for (int i = 0; i < Integer.MAX_VALUE; i++) {
            executor.execute(()->{
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //System.out.println(Thread.currentThread().getName());
            });
        }
        try {
            if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
                // 如果在指定等待时间内仍有未完成的任务，则强制关闭线程池
                executor.shutdownNow();
                // 等待线程池中的任务被中断响应
                if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
                    System.err.println("线程池未能完全关闭");
                }
                System.out.println("线程池已经安全的关闭了");
            }
        } catch (InterruptedException e) {
            // 处理中断异常
            executor.shutdownNow();
            Thread.currentThread().interrupt();
        }*/

        ThreadPoolExecutor executorOne = new ThreadPoolExecutor(5, 5, 1,
                TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(20),new CustomizableThreadFactory("yesido-Thread-pool"));
        executorOne.execute(()->{
            System.out.println(Thread.currentThread().getName()+"关注公众号：捡田螺的小男孩");
            //throw new NullPointerException();
        });
        executorOne.shutdown();


    }
}
